import {RouteRecordRaw,createRouter,createWebHistory} from 'vue-router'
import {ElMessage} from 'element-plus'
import api from '@/api'
const routes:Array<RouteRecordRaw>=[
    {
        path:'/login',
        component:()=>import('../views/Login.vue')
    },
    {
        name:'home',
        path:'/',
        component:()=>import('../views/Home.vue')
    }
]
const router=createRouter({
    routes,
    history:createWebHistory()
})
//路由全局守卫
router.beforeEach(async(to,from,next)=>{
  if(to.path==="/login"){
    next()
  }else{
    const user=localStorage.getItem('user')
    if(!user){
        ElMessage.warning('请先登录')
        next("/login")
    }else{
        const result=await api.loginModule.getAdminInfoByToken()
        if(result.data.code===401){
            ElMessage.warning('token失效')
            next("/login")
        }else if(result.data.code===200){
            next()
        }
    }
  }    
})
export default router